package com.malex.mxtracker.component;

import com.malex.mxtracker.MxTracker;
import com.malex.mxtracker.bean.Operation;
import com.malex.mxtracker.bean.OperationProvider;
import com.malex.mxtracker.bean.Project;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.Model;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Manusovich Alexander
 */
public class OperationsPanel extends Panel {

    public OperationsPanel(String id, final Project p) {
        super(id);

        ArrayList<IColumn<Operation>> columns = new ArrayList<IColumn<Operation>>();
        columns.add(new PropertyColumn<Operation>(Model.of("date"), "date"));
        columns.add(new PropertyColumn<Operation>(Model.of("value"), "value"));
        columns.add(new PropertyColumn<Operation>(Model.of("description"), "description"));

        OperationProvider operationProvider = new OperationProvider(p);
        DefaultDataTable table = new DefaultDataTable<Operation>(
                "operations", columns, operationProvider, 10);
        add(table);

        Image dynamicImage1 = getImage(p, "chart1", "http://chart.apis.google.com/chart?chtt=Incoming&cht=lc&chxt=x,y&chxl=0:|Jan|July|Jan|July|Jan&chs=350x100&chd=t:100,20,10&chxr=1,10,2000");
        Image dynamicImage2 = getImage(p, "chart2", "http://chart.apis.google.com/chart?chtt=Expense&cht=lc&chxt=x,y&chxl=0:|Jan|July|Jan|July|Jan&chs=350x100&chd=t:40,20,40,50&chxr=1,10,2000");
        dynamicImage1.setOutputMarkupId(true);
        dynamicImage2.setOutputMarkupId(true);
        add(dynamicImage1);
        add(dynamicImage2);
    }

    private Image getImage(final Project p, final String id, final String url) {
        Image dynamicImage = new Image(id);
        dynamicImage.add(new AttributeModifier("src", true, new AbstractReadOnlyModel() {
            private static final long serialVersionUID = 1L;

            /**
             * {@inheritDoc}
             */
            @Override
            public final Object getObject() {
                List<Float> vectorFloat = new ArrayList<Float>();
                if (p != null) {
                    List<Operation> ops = MxTracker.readOperations("" + p.getId());
                    for (Operation o : ops) {
                        Float value = new Float(Math.abs(o.getValue()));
                        vectorFloat.add(value);
                    }
                }
                StringBuffer buff = new StringBuffer();
                for (Float v : vectorFloat) {
                    if (buff.length() > 0) {
                        buff.append(",");
                    }
                    buff.append(v);
                }

                return url;
            }
        }));
        return dynamicImage;
    }
}